//
//  SchoolManageViewController.swift
//  HoneyBee
//
//  Created by point on 2018/1/30.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
private let cellID = "kCellID"
class SchoolManageViewController: BaseViewController {
    
    var currentModel = SchoolDetailModel()
    
    var type:Int = 0  // 1去申请 2认证中 3已认证
    
    lazy var btn:UIButton = UIButton(type: .custom)
    
    fileprivate lazy var tableView : UITableView = {
        let tableView = UITableView(frame: CGRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(MyTableViewCell.self, forCellReuseIdentifier: cellID)
        return tableView
    }()
    
    var modelArr:[String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        modelArr = ["查看机构全部信息","机构位置信息","机构基本信息","机构详情描述","机构校园角"]
        
    }
    
    @objc func clickRZ() {
        let vc = ApplyAuthenticationViewController()
        vc.backBlock = { [weak self]  in
            self?.btn.setTitle("审核中", for: .normal)
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
}


// MARK:- 原生代理
extension SchoolManageViewController:UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! MyTableViewCell
        cell.configModel(modelArr: modelArr, index: indexPath)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44;
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.item == 0 { //机构详情
            let vc = SchoolDetailViewController()
            vc.schoolID = String(UserInfo.shareInstance.userModel().institutionId)
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        
        if indexPath.item == 1 { //地址信息
            let vc = SchoolAddViewController()
            vc.type = 1
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        
        if indexPath.item == 4 { //校园角
            let vc = SchoolAddHornViewController()
            vc.schoolID = UserInfo.shareInstance.userModel().institutionId
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }else {
            push(indexPath: indexPath)
        }
        
    }
    
    
    fileprivate func push(indexPath:IndexPath) {
        
        let userModel = UserInfo.shareInstance.userModel()
        let dict = ["userId":userModel.id,
                    "id":userModel.institutionId,
                    "timestamp":"1",
                    "sign":"1",
                    ] as [String : Any]
        
        TempNetTool.get(kHost + kgetInstitutionInfo, parameters: dict, progress: nil, success: { [weak self ](result) in
            self?.hudHide()
            guard let resultDict = result as? [String : NSObject] else { return }
            guard let data = resultDict["data"] as? [String : NSObject] else { return }
            self?.currentModel = SchoolDetailModel.tmSpeelBreak(data)
            self?.currentModel.modify = true
            
            if indexPath.item == 3 { //详情
                self?.navigationController?.dcPushORPop((self?.classForCoder)!, "SchoolAddDetailViewController", self?.currentModel)
                return
            }
            
            if indexPath.item == 2 { //详情
                self?.navigationController?.dcPushORPop((self?.classForCoder)!, "SchoolAddSecondViewController",self?.currentModel)
                return
            }
            
        }) { [weak self](error) in
            self?.hudHide()
        }
    }
    
}

// MARK:- 设置UI
extension SchoolManageViewController {
    fileprivate func setupUI(){
        self.title = "机构管理"
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(LayoutCommon.navHeight)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        
        view.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().inset(30)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().inset(150)
        }
        
        if type == 1 {
            btn.setTitle("认证机构", for: .normal)
        }else {
            btn.setTitle("审核中", for: .normal)
        }
        
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.backgroundColor = kColorNavBarBG
        btn.fastCornerRadius(value: 4)
        btn.addTarget(self, action: #selector(clickRZ), for: .touchUpInside)
    }
}
